package com.isoft.mapper;

import com.isoft.entity.DfmePokeEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface DfmePokeMapper {
    /**
     * 插入消息元数据
     * @param entity DfmePokeEntity 对象
     */
    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId")
    void insertMeta(DfmePokeEntity entity);

    /**
     * 插入航班基础信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO FLIGHT_BASE(" +
            "FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, STATUS, INTERNAL_STATUS, " +
            "RELEASE_TIME, MANUAL_STATUS, MANUAL_INTERNAL_STATUS) " +
            "VALUES(" +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "#{dflt.status,jdbcType=VARCHAR}, " +
            "#{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "TO_TIMESTAMP(#{dflt.pokeTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalStatus,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "dflt.flightId")
    int insertFlightBase(DfmePokeEntity entity);

    /**
     * 插入航班航线信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert({
            "<script>",
            "INSERT INTO FLIGHT_ROUTE(ROUTE_ID, FLIGHT_ID, AIRPORT_CODE, AIRPORT_SEQ, SCHEDULED_TIME, ESTIMATED_TIME, ACTUAL_TIME) ",
            "VALUES ",
            "<foreach collection='routes' item='route' separator=','>",
            "(SEQ_ROUTE.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{route.airportCode,jdbcType=VARCHAR}, ",
            "#{route.airportSequence,jdbcType=NUMERIC}, ",
            "TO_TIMESTAMP(#{route.scheduledTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{route.estimatedTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{route.actualTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))",
            "</foreach>",
            "</script>"
    })
    int insertFlightRoute(DfmePokeEntity entity);

    /**
     * 插入登机口信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert({
            "<script>",
            "INSERT INTO GATE(GATE_ID, FLIGHT_ID, GATE_NUMBER, GATE_TYPE) ",
            "VALUES ",
            "<foreach collection='gates' item='gate' separator=','>",
            "(SEQ_GATE.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{gate.gateNumber,jdbcType=VARCHAR}, ",
            "#{gate.gateType,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    int insertGates(DfmePokeEntity entity);

    /**
     * 插入值机柜台信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert({
            "<script>",
            "INSERT INTO CHECKIN_COUNTER(COUNTER_ID, FLIGHT_ID, COUNTER_NUMBER, START_TIME, END_TIME, COUNTER_TYPE) ",
            "VALUES ",
            "<foreach collection='counters' item='counter' separator=','>",
            "(SEQ_COUNTER.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{counter.counterNumber,jdbcType=VARCHAR}, ",
            "TO_TIMESTAMP(#{counter.startTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{counter.endTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "#{counter.counterType,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    int insertCounters(DfmePokeEntity entity);

    /**
     * 插入行李转盘信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO BAGGAGE_CLAIM(BELT_ID, FLIGHT_ID, BELT_NUMBER, BELT_CODE, BELT_ATTRIBUTE, " +
            "ESTIMATED_START_TIME, ESTIMATED_END_TIME, REAL_START_TIME, REAL_END_TIME, BELT_SECTION) " +
            "VALUES(SEQ_BELT.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{baggage.beltNumber,jdbcType=VARCHAR}, " +
            "#{baggage.beltCode,jdbcType=VARCHAR}, " +
            "#{baggage.beltAttribute,jdbcType=VARCHAR}, " +
            "TO_TIMESTAMP(#{baggage.estimatedStartTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "TO_TIMESTAMP(#{baggage.estimatedEndTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "TO_TIMESTAMP(#{baggage.realStartTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "TO_TIMESTAMP(#{baggage.realEndTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{baggage.beltSection,jdbcType=VARCHAR})")
    int insertBaggage(DfmePokeEntity entity);

    /**
     * 插入航班异常信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO FLIGHT_IRREGULARITY(FLIGHT_ID, ABNORMAL_STATUS, ABNORMAL_REASON, " +
            "INTERNAL_STATUS, INTERNAL_REASON, MANUAL_ABNORMAL_STATUS, MANUAL_ABNORMAL_REASON, " +
            "MANUAL_INTERNAL_STATUS, MANUAL_INTERNAL_REASON) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{irregularity.abnormalStatus,jdbcType=VARCHAR}, " +
            "#{irregularity.abnormalReason,jdbcType=VARCHAR}, " +
            "#{irregularity.internalStatus,jdbcType=VARCHAR}, " +
            "#{irregularity.internalReason,jdbcType=VARCHAR}, " +
            "#{irregularity.manualAbnormalStatus,jdbcType=VARCHAR}, " +
            "#{irregularity.manualAbnormalReason,jdbcType=VARCHAR}, " +
            "#{irregularity.manualInternalStatus,jdbcType=VARCHAR}, " +
            "#{irregularity.manualInternalReason,jdbcType=VARCHAR})")
    int insertIrregularity(DfmePokeEntity entity);

    /**
     * 插入登机时间信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert({
            "<script>",
            "INSERT INTO BOARDING_TIME(TIME_ID, FLIGHT_ID, BOARDING_TYPE, BOARDING_TIME) ",
            "VALUES ",
            "<foreach collection='boardingTimes' item='time' separator=','>",
            "(SEQ_BOARDING_TIME.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{time.boardingType,jdbcType=VARCHAR}, ",
            "TO_TIMESTAMP(#{time.boardingTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))",
            "</foreach>",
            "</script>"
    })
    int insertBoardingTimes(DfmePokeEntity entity);

    /**
     * 插入共享航班信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert({
            "<script>",
            "INSERT INTO SHARED_FLIGHT(SHARE_ID, FLIGHT_ID, AIRLINE_CODE, FLIGHT_NUMBER) ",
            "VALUES ",
            "<foreach collection='sharedFlights' item='sharedFlight' separator=','>",
            "(SEQ_SHARED_FLIGHT.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{sharedFlight.airlineCode,jdbcType=VARCHAR}, ",
            "#{sharedFlight.flightNumber,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    int insertSharedFlights(DfmePokeEntity entity);

    /**
     * 插入特殊旅客信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO SPECIAL_PASSENGER(PASSENGER_ID, FLIGHT_ID, VIP_COUNT, SPECIAL_NEEDS) " +
            "VALUES(SEQ_PASSENGER.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{specialPassengers.vipCount,jdbcType=NUMERIC}, " +
            "#{specialPassengers.specialNeeds,jdbcType=VARCHAR})")
    int insertSpecialPassengers(DfmePokeEntity entity);

    /**
     * 插入飞机信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO AIRCRAFT(AIRCRAFT_ID, FLIGHT_ID, AIRCRAFT_TYPE, AIRCRAFT_NUMBER) " +
            "VALUES(SEQ_AIRCRAFT.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{aircraft.aircraftType,jdbcType=VARCHAR}, " +
            "#{aircraft.aircraftNumber,jdbcType=VARCHAR})")
    int insertAircraft(DfmePokeEntity entity);

    /**
     * 插入航班到达信息
     * @param entity DfmePokeEntity 对象
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO FLIGHT_ARRIVAL(ARRIVAL_ID, FLIGHT_ID) " +
            "VALUES(SEQ_ARRIVAL.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC})")
    int insertArrival(DfmePokeEntity entity);

    @Select("SELECT COUNT(1) FROM FLIGHT_BASE WHERE FLIGHT_FILE_ID = #{dflt.flightFileId}")
    int existsFlightBase(@Param("dflt") DfmePokeEntity.Dflt dflt);
}